package com.zhike.b2b.user.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.saas.biz.commen.CommMethodMgr;
import com.saas.biz.commen.SendMail;
import com.saas.biz.commen.mail.ReadHelper;
import com.zhike.b2b.dao.DbExecute;

public class RegMail
{
  Logger log = LoggerFactory.getLogger(RegMail.class);
  public static HashMap map = null;
  public static List paramList = null;
  public int ipox = 0;
  private String picPath1 = "http://localhost/include/email/images/logo.gif";
  private String picPath2 = "http://localhost/include/email/images/send.gif";

  public void getParam()
  {
    DbExecute localDbexecute = new DbExecute();
    String str = "select param_value,param_name from site_parameter";
    paramList = new ArrayList();
    paramList = localDbexecute.selBizQuerySql(str);
  }

  public String htmlString(String paramString1, String paramString2, String paramString3)
  {
    StringBuilder localStringBuilder = new StringBuilder("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
    localStringBuilder.append("<html><head>");
    localStringBuilder.append("<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />");
    localStringBuilder.append("<title> </title>");
    localStringBuilder.append("<style type='text/css'>");
    localStringBuilder.append("body{margin:0px;padding:0px;font-size:14px;line-height:16px; background:#fff;}");
    localStringBuilder.append("ul,dl,dt{padding:0px; margin:0px; list-style-type:none;}");
    localStringBuilder.append("div,form,ul,ol,li,dl,dt,h1,h2,h3,h4,dd,p,select,img,span,table,td,th,tr,dl,dt{margin:0px;padding:0; border:0; COLOR: #000;}");
    localStringBuilder.append("A:link,A:visited{color:#333230;text-decoration: underline;}");
    localStringBuilder.append("A:active{color:#333230;text-decoration:none;}");
    localStringBuilder.append("A:hover{color:#FF8000; text-decoration: underline;}");
    localStringBuilder.append(".logo{padding:30px 0px 20px 20px;}");
    localStringBuilder.append(".border{border:5px solid #FF7300;padding:30px; text-align:left;}");
    localStringBuilder.append(".box{border:2px solid #ccc; text-align:center;padding:16px;}");
    localStringBuilder.append(".border span{color:#FF7300}");
    localStringBuilder.append(".button{width:176px;height:63px; font-weight:bold; line-height:60px; font-size:32px;background:url(" + this.picPath2 + ") no-repeat left top; margin:0 auto}");
    localStringBuilder.append(".button A:link,.button A:visited,.button A:active{color:#000;text-decoration: underline;}");
    localStringBuilder.append(".button A:hover{color:#FF8000; text-decoration: underline;}");
    localStringBuilder.append("</style></head><body>");
    localStringBuilder.append("<table width='570' border='0' align='center' cellpadding='0' cellspacing='0'>");
    localStringBuilder.append("<tr><td class='logo'><img src='" + this.picPath1 + "' width='163' height='36' /></td></tr>");
    localStringBuilder.append("<tr><td class='border'>");
    localStringBuilder.append("尊敬的" + paramString3 + "：<br/><br/>");
    localStringBuilder.append("感谢您注册同徽B2B会员（会员登录名：" + paramString3 + "）<br/><br/>");
    localStringBuilder.append("<span>您注册时填写的邮箱：" + paramString1 + " 还未通过验证！</span><br/><br/>");
    localStringBuilder.append("<div style='clear:both'></div>");
    localStringBuilder.append("<div class='box'>");
    localStringBuilder.append("<span style='font-size:16px; font-weight:bold; line-height:14px;'>请确认此邮箱是您本人使用，<br/><br/>以保证潜在的商业伙伴联系到您！</span><br/>");
    localStringBuilder.append("<div style='clear:both'></div><br/>");
    localStringBuilder.append("<div class='button'><a href='" + paramString2 + "'>点此确认</a></div>");
    localStringBuilder.append("</div>");
    localStringBuilder.append("<div style='clear:both'></div><br/>");
    localStringBuilder.append("看不到或者无法点击上面的按钮？<br/><br/>");
    localStringBuilder.append("您可以将下面的链接复制到浏览器中访问，同样能完成验证！<br/><br/>");
    localStringBuilder.append("<a href='" + paramString2 + "'>" + paramString2 + "</a>\t<br/><br/>");
    localStringBuilder.append("如有任何疑问，欢迎给我们留言，我们将尽快给您回复！<br/><br/>");
    localStringBuilder.append("<div style='clear:both'></div>");
    localStringBuilder.append("<div style='float:right'>中国交通材料网</div>");
    localStringBuilder.append("</td></tr>");
    localStringBuilder.append("</table>");
    localStringBuilder.append("</body></html>");
    return localStringBuilder.toString();
  }

  public String getMailContent(String paramString, HashMap paramHashMap)
  {
    map = paramHashMap;
    ReadHelper localReadHelper = new ReadHelper();
    String str1 = localReadHelper.read(paramString);
    String str2;
    String str3;
    while (str1.indexOf("{#MAIL:") != -1)
    {
      str2 = replaValue(str1);
      str3 = str2.replace("{#MAIL:", "");
      str3 = str3.replace("#}", "").toLowerCase();
      String str4;
      if (str3.toLowerCase().equals("logourl"))
      {
        str4 = getParamValue("logolink");
        if (str4.indexOf("http://") < 0)
          str4 = "http://" + str4;
        str1 = str1.replace(str2, str4 + getParamValue("logourl"));
      }
      else if (str3.toLowerCase().equals("logoemail"))
      {
        str4 = getParamValue("logolink");
        if (str4.indexOf("http://") < 0)
          str4 = "http://" + str4;
        str1 = str1.replace(str2, str4 + "/templates/email/logo1.gif");
      }
      else
      {
        str1 = str1.replace(str2, getParamValue(str3));
      }
    }
    while (str1.indexOf("{$REGINFO:") != -1)
    {
      str2 = replaValue2(str1);
      str3 = str2.replace("{$REGINFO:", "");
      str3 = str3.replace("$/}", "").toLowerCase();
      str1 = str1.replace(str2, getRegInfo(str3));
    }
    return str1;
  }

  public String getParamValue(String paramString)
  {
    if (paramList == null)
      getParam();
    getParam();
    Object localObject = "";
    if (paramList != null)
      for (int i = 0; i < paramList.size(); i++)
      {
        String str1 = "";
        String str2 = "";
        HashMap localHashMap = (HashMap)paramList.get(i);
        if (localHashMap.get("param_name") != null)
          str1 = localHashMap.get("param_name").toString();
        if (localHashMap.get("param_value") != null)
          str2 = localHashMap.get("param_value").toString();
        if (!str1.equals(paramString))
          continue;
        localObject = str2;
        break;
      }
    return (String)localObject;
  }

  public String getRegInfo(String paramString)
  {
    String str = "";
    if (map != null)
      return map.get(paramString) != null ? map.get(paramString).toString() : str;
    return str;
  }

  public String replaValue(String paramString)
  {
    this.ipox = paramString.indexOf("{#MAIL:");
    String str1 = paramString.substring(this.ipox).trim();
    String str2 = "";
    String str3 = "#}";
    if (this.ipox != -1)
    {
      this.ipox = 0;
      str2 = str1.substring(0, str1.indexOf(str3) + str3.length()).trim();
    }
    return str2;
  }

  public String replaValue2(String paramString)
  {
    this.ipox = paramString.indexOf("{$REGINFO:");
    String str1 = paramString.substring(this.ipox).trim();
    String str2 = "";
    String str3 = "$/}";
    if (this.ipox != -1)
    {
      this.ipox = 0;
      str2 = str1.substring(0, str1.indexOf(str3) + str3.length()).trim();
    }
    return str2;
  }

  public boolean setmail()
    throws Exception
  {
    map = new HashMap();
    map.put("user_name", "user_name");
    map.put("cust_name", "cust_name");
    map.put("logourl", "logourl");
    map.put("path", "panyunfeng:8080");
    map.put("email", "email");
    String str1 = new CommMethodMgr().selStrByInfoId("site_parameter", "param_value", "param_name", "emailtemp");
    String str2 = new RegMail().getMailContent(str1, map);
    boolean bool = new SendMail().sendMailHtml("panyunfen1987@126.com", "tonghui", str2);
    if (bool == true)
      log.debug("邮箱验证发送成功。。。。。。。。。。。。。。。。。。。。。。。。。。");
    else
      log.debug("邮箱注册验证发送失败。。。。。。。。。。。。。。。。。。。。。。。。");
    return bool;
  }
}

